package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: itcast_health
 * @create: 2019-10-20 15:35
 * @author: li he
 * @description:
 */
@RestController
@RequestMapping("checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    @RequestMapping("add")
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    public Result add(Integer[] checkItemIds, @RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.add(checkItemIds,checkGroup);
            return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("findPage")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            return checkGroupService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("CheckGroupController.findPage");
            return new PageResult(0L,null);
        }
    }
    @RequestMapping("findById")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findById(Integer id){
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.QUERY_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("findCheckItemIdsById")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findCheckItemIdsById(Integer id){
        try {
            List<Integer> ids = checkGroupService.findCheckItemIdsById(id);
            return new Result(true,null,ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,null);
        }
    }
    @RequestMapping("edit")
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    public Result edit(Integer[] checkItemIds, @RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.edit(checkItemIds,checkGroup);
            return new  Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new  Result(false,MessageConst.EDIT_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("delById")
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    public Result delById(Integer id){
        try {
            checkGroupService. delById(id);
            return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("findAll")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findAll(){
        try {
            List<CheckGroup> checkGroupList = checkGroupService.findAll();
            return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.QUERY_CHECKGROUP_FAIL);
        }
    }
}